473,422 Members | 2,235 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,422 software developers and data experts.

remove whitespace from directory path

i am trying to get a user input and use that input as part of a directory path. i have tried many way of doing this but cant get rid of the space.

Expand|Select|Wrap|Line Numbers
  1. accfile1 = raw_input("Enter file name:")
  2. print "C:\Fahad\ACC\Output\ ".replace(' ', ''),accfile1
My output looks like this
C:\Fahad\ACC\Output\ test.txt

notice the white space before test.txt. I want it to look like this
C:\Fahad\ACC\Output\test.txt

I have tried the replace method as shown and a couple other methods i found online but the annoying white space wont go away.

any help would be greatly appreciated
Jun 11 '09 #1
5 8061
bvdet
2,851 Expert Mod 2GB
The problem is not the space, but the backslash character (\). It is used to escape special characters such as newlines and tabs. Example:
Expand|Select|Wrap|Line Numbers
  1. >>> print 'abc\test\Bxxx\n1234'
  2. abc    est\Bxxx
  3. 1234
  4. >>> print 'abc\\test\\Bxxx\\n1234'
  5. abc\test\Bxxx\n1234
  6. >>> 
For a literal backslash, escape it with "\\" or input forward slashes.

-BV
Jun 11 '09 #2
what if i was trying to use the string as a path name. is it even possible to store a pathname as a string. for example if i wanted to assign C:\fahad\test.txt as a string and then call on it.

an overall picture of what i am trying to do is ask the user for a file name at which the user will enter for example "test.txt" . it may not be .txt everytime could be other file names whatever the user enters.

now i want to use what the user entered as part of the pathname and store the entire pathname in one variable.

Expand|Select|Wrap|Line Numbers
  1. accfile = raw_input("Enter file name: ")
  2. path =  str("C:\Fahad\ACC\Output\\"), accfile
  3. print path
so if the user enters test.txt as the raw input

the output is coming out as: ('C:\\Fahad\\ACC\\Output\\', 'test.txt')
instead i need it to come out clean as "C:\Fahad\ACC\Output\test.txt"

it looks like whats after the comma is being considered as separate string wheras i want it to be part of the original string.
Jun 11 '09 #3
bvdet
2,851 Expert Mod 2GB
The strings must use "\\" or "/" to represent a valid path. Try using os.path.join() or string method join(). Example:
Expand|Select|Wrap|Line Numbers
  1. >>> import os
  2. >>> dir_path = "C:\\Fahad\\ACC\\Output"
  3. >>> accfile = raw_input("Enter file name: ")
  4. >>> print accfile
  5. test.txt
  6. >>> os.path.join(dir_path, accfile)
  7. 'C:\\Fahad\\ACC\\Output\\test.txt'
  8. >>> print os.path.join(dir_path, accfile)
  9. C:\Fahad\ACC\Output\test.txt
  10. >>> dir_path = "C:/Fahad/ACC/Output"
  11. >>> '/'.join([dir_path, accfile])
  12. 'C:/Fahad/ACC/Output/test.txt'
  13. >>> 
Jun 11 '09 #4
Thanks a lot man it worked, you are incredible at this.
Jun 11 '09 #5
bvdet
2,851 Expert Mod 2GB
You are welcome. Keep posting.

-BV
Jun 11 '09 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Thomas Mlynarczyk | last post by:
Which is the simplest way to remove all whitespace from a string? Is there a simpler method than a regex replace? Or how can I tell a regex pattern to ignore all whitespace in my subject string?...
2
by: kbass | last post by:
I would like to remove file that are older than 7 days old from a directory. I can do this in shell script rather easy but I would like to integrate this functionality into my Python program. How...
23
by: Stan Cook | last post by:
I was trying to take a list of files in a directory and remove all but the ".dbf" files. I used the following to try to remove the items, but they would not remove. Any help would be greatly...
5
by: Andy Leszczynski | last post by:
How remove directories with the content in the platform independent way? Is the API for that? Thx, A.
19
by: Steve Franks | last post by:
I am using VS.NET 2005 beta 2. When I run my project locally using the default ASP.NET Development Web Server it runs using a root address like this: http://localhost:11243/testsite/ However...
10
by: Brian Gruber | last post by:
Hi, I'm looking for a way to rename a whole directory of files in short order. The files in the directory have different lengths, however all of them end with _xxxx the x's represent a randomly...
15
by: morleyc | last post by:
Hi, i would like to remove a number of characters from my string (\t \r \n which are throughout the string), i know regex can do this but i have no idea how. Any pointers much appreciated. Chris
2
by: Javier | last post by:
Hello all, I'm trying to remove a hidden file (in UNIX) using the Boost::filesystem library. This is what I have: #include <boost/filesystem/operations.hpp> namespace...
4
by: =?Utf-8?B?U3VzaGlTZWFu?= | last post by:
I have some site on asp.net 2.0 and it works with directories - create and remove them with files inside. I never have any problems with create, but a lot of times when I am tring remove directory...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.